from django.db import models


# 权限管理员模型类
class AdminUser(models.Model):
    phone_number = models.CharField(max_length=11, unique=True, verbose_name='手机号')
    name = models.CharField(max_length=50, verbose_name='姓名')
    entry_date = models.DateField(verbose_name='入职日期')
    role = models.ForeignKey('Roles', on_delete=models.SET_NULL, null=True, blank=True, verbose_name='角色')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '权限管理员'
        verbose_name_plural = '权限管理员'


# 角色表模型类
class Roles(models.Model):
    role_name = models.CharField(max_length=50, unique=True, verbose_name='角色名')

    def __str__(self):
        return self.role_name

    class Meta:
        verbose_name = '角色'
        verbose_name_plural = '角色'


# 资源表模型类
class Resources(models.Model):
    resource_name = models.CharField(max_length=100, verbose_name='资源名')
    url = models.CharField(max_length=200, verbose_name='URL')
    parent = models.ForeignKey('self', on_delete=models.SET_NULL, null=True, blank=True, related_name='children',
                               verbose_name='父资源')

    def __str__(self):
        return self.resource_name

    class Meta:
        verbose_name = '资源'
        verbose_name_plural = '资源'


# 角色资源表模型类
class RoleResource(models.Model):
    role = models.ForeignKey(Roles, on_delete=models.CASCADE, verbose_name='角色')
    resource = models.ForeignKey(Resources, on_delete=models.CASCADE, verbose_name='资源')

    def __str__(self):
        return f'{self.role.role_name} - {self.resource.resource_name}'

    class Meta:
        verbose_name = '角色资源关联'
        verbose_name_plural = '角色资源关联'
